<!--待尽调-->
<template>
  <div>
    <el-form :model="checkForm" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="办理结果" prop="approveStatus"  :rules="[{ required: true, message: '请选择办理结果', trigger: 'change' }]">
        <el-radio-group v-model="checkForm.approveStatus" @change="handleChange">
          <el-radio label="3" class="red" >退件</el-radio>
          <el-radio label="5" class="red" >拒件</el-radio>
          <el-radio label="7" class="red" >转派</el-radio>
          <el-radio label="9" class="red" >通过</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="转派" prop="checkerId" label-width="130px"  v-if="checkForm.approveStatus === '7'" required>
        <el-select v-model="checkForm.checkerId" placeholder="请选择">
          <el-option
            v-for="item in sendCheckPersons"
            :key="item.id"
            :label="item.nameAndMobile"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="补充资料" prop="checkerId" label-width="130px"  v-if="checkForm.approveStatus === '1'" required>
        <el-select v-model="checkForm.checkerId" placeholder="请选择">
          <el-option v-for="item in supplyUser" :key="item.id" :label="item.displayName+'-'+item.roleName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>

      

      <el-form-item label="下户开始时间" prop="nextStartTime" label-width="130px" v-if="checkForm.approveStatus === '9'" 
        :rules="[{ required: true, message: '下户开始时间不能为空', trigger: 'blur' }]"
      >
        <el-date-picker v-model="checkForm.nextStartTime" type="datetime" placeholder="选择日期" 
        style="width: 300px" value-format="yyyy-MM-dd HH:mm:ss"
        :picker-options="pickerStartOption()"
        > </el-date-picker>
      </el-form-item>

      

      <el-form-item label="下户结束时间" prop="nextEndTime" label-width="130px"  v-if="checkForm.approveStatus === '9'" 
        :rules="[{ required: true, message: '下户结束时间不能为空', trigger: 'blur' }]"
      >
        <el-date-picker v-model="checkForm.nextEndTime" type="datetime" placeholder="选择日期" 
        style="width: 300px" value-format="yyyy-MM-dd HH:mm:ss"
        :picker-options="pickerEndOption()"
        > </el-date-picker>
      </el-form-item>

      

      <el-form-item label="提审时间" prop="arraignmentTime" label-width="130px" v-if="checkForm.approveStatus === '9'" 
        :rules="[{ required: true, message: '提审时间不能为空', trigger: 'blur' }]"
      >
        <el-date-picker v-model="checkForm.arraignmentTime" type="datetime" placeholder="选择日期" 
        style="width: 300px" value-format="yyyy-MM-dd HH:mm:ss"
        :picker-options="pickerOption"
        > </el-date-picker>
      </el-form-item>

      <el-form-item label="反馈信息" prop="opinion" label-width="130px"  v-if="checkForm.approveStatus != '7'">
        <el-input type="textarea" :rows="7" v-model="checkForm.opinion" style="width: 300px;"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')" v-loading="loading">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  import { waitDueDiligence, listRedePloyUser,getTaskCandiates } from '@/api/project'
export default {
  props: {
     projectId: undefined,
     taskId: undefined
  },
  data() {
    return {
      pickerOption: {
        disabledDate(current) {
          return current && current.getTime() > new Date().getTime();
        },
      },
      checkForm: {
        opinion: undefined,
        checkerId: undefined,
        projectId: undefined,
        approveStatus: undefined,
        arraignmentTime:undefined,
        nextStartTime:undefined,
        nextEndTime:undefined
      },
      sendCheckPersons: [],
      supplyUser: [],
      value: '',
      loading: false
    };
  },
   created() {
    // 初始化数据
    this.supplyUsers();
    this.listRedePloyUser();
  },
  methods: {
    pickerStartOption() {
        // this.oldTime 是上一条数据的开始时间
        let date = this.checkForm.nextEndTime?new Date(this.checkForm.nextEndTime):new Date()
        let minutes = date.getMinutes()
        let s = date.getSeconds()
        // h和m 是将日期只取时分
        let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        let m = (minutes < 10 ? '0' + minutes : minutes) + ':';
        s = (s < 10 ? '0' + s : s);
        // pickerDate 判断当前选择时间是否等于上条数据时间
        // 若等于时，限制时间从00：00：00 - 上条数据开始时间，不等于时，24小时可选
        let pickerDate = this.checkForm.nextEndTime?date.toDateString() === new Date(this.checkForm.nextEndTime).toDateString() :date.toDateString() === new Date().toDateString()
        return {
          // 日期限制
          disabledDate: (time) => {
            let old = this.oldTime
            if(this.checkForm.nextEndTime) {
              return time.getTime() > new Date(this.checkForm.nextEndTime).getTime()
            }
            return time.getTime() > new Date().getTime()
          },
          // 时间限制
          selectableRange: pickerDate?'00:00:00 - '+ h + m  + s +'' : '00:00:00 - 23:59:59'
        }
    },
    pickerEndOption() {
        // this.oldTime 是上一条数据的开始时间
        let date =this.checkForm.nextStartTime?new Date(this.checkForm.nextStartTime) :new Date()
        let minutes = date.getMinutes()
        let s = date.getSeconds()
        // h和m 是将日期只取时分
        let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        let m = (minutes < 10 ? '0' + minutes : minutes) + ':';
        s = (s < 10 ? '0' + s : s);
        // pickerDate 判断当前选择时间是否等于上条数据时间
        // 若等于时，限制时间从00：00：00 - 上条数据开始时间，不等于时，24小时可选
        let pickerDate = this.checkForm.nextStartTime?date.toDateString() === new Date(this.checkForm.nextStartTime).toDateString(): date.toDateString() === new Date().toDateString();
        return {
          // 日期限制
          disabledDate: (time) => {
            let old = this.checkForm.nextStartTime
            if(this.checkForm.nextStartTime) {
              return time.getTime() < new Date(old).getTime() || time.getTime() > new Date().getTime()
            }
            return time.getTime() > new Date().getTime() 
          },
          // 时间限制
          selectableRange: pickerDate?this.checkForm.nextStartTime?''+ h + m  + s +' - 23:59:59' :'00:00:00 - '+ h + m  + s +'' : '00:00:00 - 23:59:59'
        }
    },
    submitForm(checkForm) {
      this.$refs[checkForm].validate((valid) => {
          if (valid) {
            this.$confirm('你确定要提交吗?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              // 开始提交信息
              let data = {
                projectId: this.projectId,
                taskId:this.taskId,
                opinion: this.checkForm.opinion,
                checkerId: this.checkForm.checkerId,
                approveStatus: this.checkForm.approveStatus,
                approvalDTO: {
                  arraignmentTime:this.checkForm.arraignmentTime,
                  nextStartTime:this.checkForm.nextStartTime,
                  nextEndTime:this.checkForm.nextEndTime,
                  projectId: this.projectId
                }
                
              }
              this.loading = true;
              waitDueDiligence(data).then((res) => {
                  this.loading = false;
                    this.$notify.success({
                      title: '成功',
                      message: '操作成功'
                    })
                  this.$store.dispatch("tagsView/delView", this.$route);
                  this.$router.go(-1);
                }).catch(() => {
                  this.loading = false;
                })
            })
          }
      })
    },
    listRedePloyUser() {
      // 查询数据 构造页面
      listRedePloyUser('尽职调查岗').then((data) => {
        this.sendCheckPersons = data.data;
        console.log('响应数据:%o', data);
      });
    },
    supplyUsers() {
      // 查询数据 构造页面
      getTaskCandiates(this.taskId)
        .then((data) => {
          this.supplyUser = data.data;
          console.log('响应数据:%o', data);
        })
    },

    handleChange(val){
      console.log(val)
      this.restForm()
      this.checkForm.approveStatus = val
    },
    restForm(){
      this.checkForm = {
        opinion: undefined,
        checkerId: undefined,
        projectId: undefined,
        approveStatus: undefined
      }
    }
  },
};
</script>
